<?php

namespace App\Datas;

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Tools\FunTool;

class AfLog
{
    public $tableName = "af_log";

    private static $instance = null;

    private function __construct()
    {
    }

    public static function getInstance() {
        if (empty(self::$instance) && !(self::$instance instanceof AfLog)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public function getInfo(array $where) {
        $info = DB::table($this->tableName)->where($where)->first();
        return empty($info) ? [] :(array)$info;
    }

    public function addData(array $data) {
        return DB::table($this->tableName)->insert($data);
    }

    public function updateData(array $updateData,$where){
        return DB::table($this->tableName)->where($where)->update($updateData);
    }

    public function addLog($json,$platform) {
        if (empty($json)) return;
        $data = json_decode($json,true);
        if (empty($data) || !is_array($data)) {
            Log::warning(sprintf("af json error. json : %s",$json));
            return;
        }
        if (!array_key_exists("appsflyer_id",$data)) {
            Log::warning(sprintf("appsflyer_id not exist. json : %s",$json));
            return;
        }
        $appsflyer_id = $data["appsflyer_id"];
        $media_source = array_key_exists("media_source",$data) && !empty($data["media_source"]) ? $data["media_source"] : "organic";
        $info = $this->getInfo(["appsflyer_id" => $appsflyer_id,"platform" => $platform]);

        $new_media_source = $media_source;
        if ($platform == 2) { //安卓的
            if ($new_media_source == 'organic') {
                $new_media_source = 'googleadwords_int';
            }
            if ($new_media_source == 'restricted') {
                $new_media_source = 'Facebook Ads';
            }
        }

		// 根据 appsfly_id 更新 设备表 af_channel
		if(!empty($appsflyer_id) && !empty($media_source)) {
            $deviceInfo = DB::table('system_device')->where('appsfly_id',$appsflyer_id)->first();
            if ($deviceInfo) {
                DB::table('system_device')->where('id',$deviceInfo->id)->update(['af_channel'=>$new_media_source]);
            }
		}
        //更新
        if (!empty($info)) {
            if ($info["media_source"] != $media_source){
                $this->updateData([
                    "media_source" => $media_source,
                    "content" => $json,
                    'new_media_source'=> $new_media_source
                ],
                    ["id" => $info["id"]]
                );
            }
            return;
        }
        
        
        $this->addData([
            "appsflyer_id" => $appsflyer_id,
            "media_source" => $media_source,
            "platform" => $platform,
            "content" => $json,
            'create_time'=>FunTool::getReduceTime(4),
            'new_media_source' => $new_media_source,
        ]);

    }
}
